﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class PositionReposity : Reposity
    {
        public IEnumerable<Position> ListPositions(long stackId, int type)
        {
            var positions = from r in _dataContext.Positions
                         where r.StackId == stackId && r.Type == type
                        orderby r.Id ascending
                            select r;
            foreach (Position p in positions)
            {
                yield return p;
            }
        }



        public IEnumerable<PositionPicture> ListPositionPictures(long positionId)
        {
            var pictures = from r in _dataContext.PositionPictures
                         where r.PositionId == positionId
                         orderby r.Date ascending
                         select r;
            foreach (PositionPicture p in pictures)
            {
                yield return p;
            }
        }

        public Position GetPositionById(long id)
        {
            var e = from r in _dataContext.Positions
                          where id == r.Id
                          select r;

            if (e != null && e.Count() > 0)
            {
                return e.First();
            }
            else
            {
                return null;
            }
        }

        public PositionPicture GetPositionPictureById(long id)
        {
            var e = from r in _dataContext.PositionPictures
                    where id == r.Id
                    select r;

            if (e != null && e.Count() > 0)
            {
                return e.First();
            }
            else
            {
                return null;
            }
        }

        public PositionPicture GetPositionPictureByPositionIdAndDate(long positionId, DateTime date)
        {
            var e = from r in _dataContext.PositionPictures
                    where positionId == r.PositionId && date == r.Date
                    select r;

            if (e != null && e.Count() > 0)
            {
                return e.First();
            }
            else
            {
                return null;
            }
        }

        public Position CreatePosition(Position p)
        {
            _dataContext.Positions.InsertOnSubmit(p);
            SubmitChanges();
            return p;
        }

        public Position UpdatePosition(Position p)
        {
            SubmitChanges();
            return p;
        }

        public PositionPicture CreatePositionPicture(PositionPicture p)
        {
            _dataContext.PositionPictures.InsertOnSubmit(p);
            SubmitChanges();
            return p;
        }

        public PositionPicture UpdatePositionPicture(PositionPicture p)
        {
            SubmitChanges();
            return p;
        }
    }
}